﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using Eis51.CampanyCMS.Model;
using Eis51.CampanyCMS.Model.Bus;
using Eis51.CampanyCMS.Business.Bus;

namespace Eis51.Web.dataPage
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Product : IHttpHandler
    {
        private int Pid;

        public void ProcessRequest(HttpContext context)
        {
            if (!Check(context)) 
            {
                return;
            }
            context.Response.ContentType = "text/plain";
            string returnStr = string.Empty;

            LoadData(context,ref returnStr);
            context.Response.Write(returnStr);
        }

        private bool Check(HttpContext context)
        {
            if (string.IsNullOrEmpty(context.Request.QueryString["type"])
            || string.IsNullOrEmpty(context.Request.QueryString["pid"]))
            {
                return false;
            }
            return true;
        }


        private void LoadData(HttpContext context,ref string hoemData)
        {
            try
            {
                Pid = int.Parse(context.Request.QueryString["pid"]);
            }
            catch (Exception ex)
            {
                return;
            }
            switch (context.Request.QueryString["type"].ToLower())
            {
                case "product":
                    hoemData=LoadProduct();
                    return;
            }            
        }

        private string LoadProduct() {
            Products product = ProductsBLL.GetInstance().Get(new Products.Key(Pid));
            if (null != product) {
                return product.Descr;
            }
            return string.Empty;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
